from pydantic import BaseModel, Field
from datetime import datetime
from typing import List, Optional

# 字典类型基础模型
class DictTypeBase(BaseModel):
    name: str = Field(..., description="字典名称")
    code: str = Field(..., description="字典编码")
    description: Optional[str] = Field(None, description="字典描述")
    sort_order: int = Field(default=0, description="排序")
    is_active: bool = Field(default=True, description="是否激活")

# 创建字典类型请求模型
class DictTypeCreate(DictTypeBase):
    pass

# 更新字典类型请求模型
class DictTypeUpdate(BaseModel):
    name: Optional[str] = Field(None, description="字典名称")
    code: Optional[str] = Field(None, description="字典编码")
    description: Optional[str] = Field(None, description="字典描述")
    sort_order: Optional[int] = Field(None, description="排序")
    is_active: Optional[bool] = Field(None, description="是否激活")

# 字典类型响应模型
class DictTypeSchema(DictTypeBase):
    id: int
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True

# 字典项基础模型
class DictItemBase(BaseModel):
    dict_type_id: int = Field(..., description="字典类型ID")
    label: str = Field(..., description="显示标签")
    value: str = Field(..., description="值")
    description: Optional[str] = Field(None, description="描述")
    sort_order: int = Field(default=0, description="排序")
    is_active: bool = Field(default=True, description="是否激活")

# 创建字典项请求模型
class DictItemCreate(DictItemBase):
    pass

# 更新字典项请求模型
class DictItemUpdate(BaseModel):
    label: Optional[str] = Field(None, description="显示标签")
    value: Optional[str] = Field(None, description="值")
    description: Optional[str] = Field(None, description="描述")
    sort_order: Optional[int] = Field(None, description="排序")
    is_active: Optional[bool] = Field(None, description="是否激活")

# 字典项响应模型
class DictItemSchema(DictItemBase):
    id: int
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True

# 带字典项的字典类型响应模型
class DictTypeWithItems(DictTypeSchema):
    dict_items: List[DictItemSchema] = []